package chaoyue.study.thread;

import chaoyue.study.ThreadUtils;

/**
 * 守护线程
 * 直接创建的线程都是用户线程，声明周期是独立于主线程的
 * 守护线程可以随着主线程的消亡而消亡
 */
public class ThreadDaemon {
    public static void main(String[] args) {
        System.out.println("main start... " + Thread.currentThread().getName());
        Thread t1 = new Thread(() -> {
            System.out.println("thread start..." + Thread.currentThread().getName());
            ThreadUtils.sleep();
            System.out.println("thread end..." + Thread.currentThread().getName());
        });
        t1.setDaemon(true); // 设置为守护线程，主线程结束则当前线程也会结束
        t1.start();
        System.out.println("main end..." + Thread.currentThread().getName());
    }
}
